package cn.cityre.tools.crestat;

import cn.cityre.tools.db.DynamicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.HashMap;


/**
 * @author sunzhaoye
 */
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class, MongoAutoConfiguration.class,
        MongoDataAutoConfiguration.class })
@ComponentScan(basePackages = { "cn.cityre.tools", "worker", "hotline.impl"})
@Import(value = {DataSourceConfig.class})
public class Application implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setWaitForTasksToCompleteOnShutdown(false);
        return executor;
    }

    @Autowired
    Environment env;

    @Autowired
    DynamicDataSource sourceDataSource;

    @Autowired
    TaskExecutor taskExecutor;

    @Autowired
    ApplicationContext context;

    @Value("${stat.action}")
    String action;

    /**
     * 调用方法: lang=c # 计算某个月的统计数据 java -jar crestat-1.0.0.jar
     * --stat.action=hasimilarity --stat.month=201702 --db.cities.list=苏州,成都 #
     * 计算某个月至某个月的统计数据 java -jar crestat-1.0.0.jar --stat.action=hasimilarity
     * --stat.month=201602-201702 --db.cities.list=苏州,成都
     */
    @Override
    public void run(String... args) throws Exception {
        StatWorker worker;
        log.info("action {}", action);

        HashMap<String, String> workers = new HashMap<>(16);
        /*----------- 蓝睛热线中心定时任务 start -----------*/
        workers.put("hotlineWx", "worker.WxNewsWorker");
        /*----------- 蓝睛热线中心定时任务 end -----------*/


        if (!workers.containsKey(action)) {
            log.error("not support {}", action);
            return;
        }

        Class<?> c = Class.forName(workers.get(action));
        worker = (StatWorker) context.getBean(c);

        worker.beforeWork(context, env);
        worker.work();
    }

}
